package com.huawei.hianalytics.ab.bc.ab;

import android.text.TextUtils;
import android.util.Pair;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class ab implements fg {
    private static final Charset ab = Charset.forName("UTF-8");

    private Pair<byte[], byte[]> ab(String str) {
        if (TextUtils.isEmpty(str) || str.length() < 32) {
            return new Pair(new byte[0], new byte[0]);
        }
        return new Pair(de.ab(str.substring(0, 32)), de.ab(str.substring(32)));
    }

    private String ab(String str, byte[] bArr, byte[] bArr2) {
        return (bArr2 == null || str == null || bArr == null || bArr.length == 0) ? "" : ab(bArr, de.ab(bc(str, bArr, bArr2)));
    }

    private String ab(byte[] bArr, String str) {
        if (bArr == null || bArr.length == 0) {
            return str;
        }
        if (str == null) {
            str = "";
        }
        return de.ab(bArr).concat(str);
    }

    private String ab(byte[] bArr, byte[] bArr2, byte[] bArr3) {
        if (bArr.length == 0 || bArr2.length == 0 || bArr3.length == 0) {
            com.huawei.hianalytics.ab.bc.ef.ab.cd("Aes", "decrypt: parameter exception");
            return "";
        }
        try {
            Cipher instance = Cipher.getInstance("AES/CBC/PKCS5Padding");
            instance.init(2, new SecretKeySpec(bArr, "AES"), new IvParameterSpec(bArr2));
            return new String(instance.doFinal(bArr3), ab);
        } catch (NoSuchAlgorithmException e) {
            com.huawei.hianalytics.ab.bc.ef.ab.cd("Aes", "aesDecrypt(): getInstance - No such algorithm,transformation");
            return "";
        } catch (InvalidKeyException e2) {
            com.huawei.hianalytics.ab.bc.ef.ab.cd("Aes", "aesDecrypt(): init - Invalid key!");
            return "";
        } catch (InvalidAlgorithmParameterException e3) {
            com.huawei.hianalytics.ab.bc.ef.ab.cd("Aes", "aesDecrypt(): init - Invalid algorithm parameters !");
            return "";
        } catch (NoSuchPaddingException e4) {
            com.huawei.hianalytics.ab.bc.ef.ab.cd("Aes", "aesDecrypt():  No such filling parameters ");
            return "";
        } catch (BadPaddingException e5) {
            com.huawei.hianalytics.ab.bc.ef.ab.cd("Aes", "aesDecrypt(): False filling parameters!");
            return "";
        } catch (IllegalBlockSizeException e6) {
            com.huawei.hianalytics.ab.bc.ef.ab.cd("Aes", "aesDecrypt(): doFinal - The provided block is not filled with");
            return "";
        }
    }

    private byte[] bc(String str, byte[] bArr, byte[] bArr2) {
        if (TextUtils.isEmpty(str) || bArr == null || bArr.length == 0) {
            return new byte[0];
        }
        try {
            Cipher instance = Cipher.getInstance("AES/CBC/PKCS5Padding");
            instance.init(1, new SecretKeySpec(de.ab(str), "AES"), new IvParameterSpec(bArr));
            return instance.doFinal(bArr2);
        } catch (NoSuchAlgorithmException e) {
            com.huawei.hianalytics.ab.bc.ef.ab.cd("Aes", "aesEncrypt(): getInstance - No such algorithm,transformation");
            return new byte[0];
        } catch (InvalidKeyException e2) {
            com.huawei.hianalytics.ab.bc.ef.ab.cd("Aes", "aesEncrypt(): init - Invalid key!");
            return new byte[0];
        } catch (InvalidAlgorithmParameterException e3) {
            com.huawei.hianalytics.ab.bc.ef.ab.cd("Aes", "aesEncrypt(): init - Invalid algorithm parameters !");
            return new byte[0];
        } catch (NoSuchPaddingException e4) {
            com.huawei.hianalytics.ab.bc.ef.ab.cd("Aes", "aesEncrypt(): No such filling parameters ");
            return new byte[0];
        } catch (BadPaddingException e5) {
            com.huawei.hianalytics.ab.bc.ef.ab.cd("Aes", "aesEncrypt(): False filling parameters!");
            return new byte[0];
        } catch (IllegalBlockSizeException e6) {
            com.huawei.hianalytics.ab.bc.ef.ab.cd("Aes", "aesEncrypt(): doFinal - The provided block is not filled with");
            return new byte[0];
        }
    }

    public String ab(String str, String str2) {
        if (TextUtils.isEmpty(str2) || TextUtils.isEmpty(str)) {
            com.huawei.hianalytics.ab.bc.ef.ab.cd("Aes", "decrypt: content or key is empty");
            return "";
        }
        byte[] ab = de.ab(str);
        Pair ab2 = ab(str2);
        return ab(ab, (byte[]) ab2.first, (byte[]) ab2.second);
    }

    public String ab(String str, byte[] bArr) {
        return ab(str, de.ab(), bArr);
    }

    public String bc(String str, String str2) {
        try {
            return ab(str, str2.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            com.huawei.hianalytics.ab.bc.ef.ab.bc("Aes", "Unsupported Encoding Exception : utf-8");
            return "";
        }
    }
}
